//
//  HZProductDetailSecView.m
//  app
//
//  Created by 刘华舟 on 15/7/14.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HZProductDetailSecView.h"

static const NSInteger kTagButtonHeight = kHZProductDetailSecView;

@interface HZProductDetailSecView()

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) NSMutableArray *tabBtns;
@property (nonatomic, strong) UIButton *lastTabBtn;

@end

@implementation HZProductDetailSecView

- (void)drawRect:(CGRect)rect{
    [self drawHorizonLineLeft:[self.ubo fw:0] right:[self.ubo fw:0] at:[self.ubo py:floor(0.5)] color:HZ_MALL_M_SEPARATOR_CELL_LINE];
    [self drawHorizonLineLeft:[self.ubo fw:0] right:[self.ubo fw:0] at:[self.ubo uy:kTagButtonHeight py:floor(0.5)] color:HZ_MALL_M_SEPARATOR_CELL_LINE];
}



#pragma mark getter and setter
- (NSMutableArray *)tabBtns{
    if (_tabBtns == nil) {
        _tabBtns = [NSMutableArray array];
    }
    return _tabBtns;
}

#pragma mark life circle

- (instancetype)initWithFrame:(CGRect)frame{
    frame = CGRectOf([UBO_SCREEN fx:UB_LEFT fy:UB_TOP],
                     [UBO_SCREEN fw:UB_WIDTH uh:kTagButtonHeight ph:1]);
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = HZ_WHITE_COLOR;
        
        self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectOf([UBO_SCREEN fx:UB_LEFT fy:UB_TOP py:floor(0.5)],
                                                                       [UBO_SCREEN fw:UB_WIDTH uh:kTagButtonHeight])];
        [self addSubview:self.scrollView];
        
    
    }
    return self;
}

- (void)showProductDetailSecViewWithTabs:(NSArray *)tabs{
    if (tabs.count <= 0) {
        return;
    }
    
    //去除原来的按钮
    for (UIButton *btn in self.tabBtns) {
        [btn removeFromSuperview];
    }
    
    self.lastTabBtn = nil;
    
    [self.tabBtns removeAllObjects];
    
    //添加新按钮
    UIImage *noneLineImage = [[UIImage imageNamed:kImageButtonBottomLineNoneBackgroundWhite] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 1, 0, 1) resizingMode:UIImageResizingModeStretch];
    UIImage *orangeLineImage = [[UIImage imageNamed:kImageButtonBottomLineOrangeBackgroundWhite] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 1, 0, 1) resizingMode:UIImageResizingModeStretch];
    
    CGFloat btnWidth = [UBO_SCREEN fw:UB_1_3];
    if (tabs.count < 3) {
        btnWidth = [UBO_SCREEN fw:UB_WIDTH]/tabs.count;
    }
    NSInteger index = 0;
    for (NSString *tabName in tabs) {
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectOf([self.scrollView.ubo fx:UB_LEFT ox:index*btnWidth fy:UB_TOP],
                                                                 [self.scrollView.ubo ow:btnWidth fh:UB_HEIGHT])];
        [btn setTitle:tabName forState:UIControlStateNormal];
        [btn setTitle:tabName forState:UIControlStateSelected];
        [btn setTitleColor:HZ_MALL_TEXT_BLACK_COLOR forState:UIControlStateNormal];
        [btn setTitleColor:HZ_MALL_MAIN_THEME_ORANGE forState:UIControlStateSelected];
        [btn setBackgroundImage:noneLineImage forState:UIControlStateNormal];
        [btn setBackgroundImage:orangeLineImage forState:UIControlStateSelected];
        btn.tag = index;
        [btn addTarget:self action:@selector(barBtnOnClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.titleLabel.font = JKAppGlobleFontMenu;
        btn.selected = NO;
        [self.scrollView addSubview:btn];
        [self.tabBtns addObject:btn];
        
        if (index == 0) {
            self.lastTabBtn = btn;
            self.lastTabBtn.selected = YES;
        }
        
        index++;
    }
    
    self.scrollView.contentSize = [self.scrollView.ubo ow:btnWidth*tabs.count fh:UB_TOP];
}

- (void)barBtnOnClick:(UIButton *)btn{
    
    self.lastTabBtn.selected = NO;
    
    self.lastTabBtn = btn;
    
    self.lastTabBtn.selected = YES;
    
    if (self.targetCallerSecTabChanged) {
        [self.targetCallerSecTabChanged call:[NSString stringWithFormat:@"%d", btn.tag]];
    }
}

@end
